//
//  YFLoginApi.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/6.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFLoginApi.h"
#import "NSString+Extension.h"
#import "DeviceInfo.h"

@interface YFLoginApi()
@property (nonatomic, copy)NSString *account;
@property (nonatomic, copy)NSString *password;

@end
@implementation YFLoginApi

- (NSString *)requestUrl {
    return [YFLoginApi requestString:self.apiType];
}

- (id)requestArgument {
    if (self.apiType == kLoginEmailSignUpApi ) {
        return @{
                 @"email":_account,
                 @"password":_password,
                 };
    }else if (self.apiType == kLoginEmailSignInApi){
        return @{
                 @"email":_account,
                 @"pwd":[_password stringToMD5],
                 };
    }else {
        return [super requestArgument];
    }
}

// 获取跟人信息
+ (YFLoginApi *)getUserInfo {
    return [YFLoginApi requestApiWithType:kGetUserInfoApi andParam:nil];
}

// 上传个人信息
+ (YFLoginApi *)uploadUserInfoWithUserInfoItem:(UserInfoItem *)userInfoItem {
    NSArray *trainArr = [userInfoItem.trainingTarget componentsSeparatedByString:@"-"];
    NSString *training  = @"";
    if (trainArr.count == 2) {
        training = [NSString stringWithFormat:@"%zd-%@",[SquashEnum timeSectionWithString:trainArr.firstObject],[trainArr.lastObject trimingAllNumber]];
    }
    NSDictionary *dict = @{
                           @"FirstName":[NSString checkIfNullWithString:userInfoItem.FirstName],
                           @"LastName":[NSString checkIfNullWithString:userInfoItem.lastName],
                           @"Age":userInfoItem.birthYear,
                           @"Gender":userInfoItem.gender == GenderMale ? @"1":@"0",
                           @"Height":userInfoItem.height,
                           @"Weight":userInfoItem.weight,
                           @"Hand":@(userInfoItem.handBallType).stringValue,
                           @"Training":training,
                           @"Address":userInfoItem.address,
                           @"Racquet":@(userInfoItem.racquetType).stringValue,
                           @"Email":userInfoItem.email,
                           };
    YFLoginApi *saveUserInfo = [YFLoginApi requestApiWithType:kSaveUserInfoApi andParam:dict];
    return saveUserInfo;
}

// 申请上传头像的评证
+ (YFLoginApi *)applyUploadHeadIconToken {
    return [YFLoginApi requestApiWithType:kGetQiNiuUpHeadIconTokenApi andParam:nil];
}

+ (YFLoginApi *)loginApiType:(RequestApiType)apiType email:(NSString *)email password:(NSString *)password {
    YFLoginApi *loginApi = [[YFLoginApi alloc] init];
    loginApi.apiType = apiType;
    loginApi.account = email;
    loginApi.password = password;
    return loginApi;
}

+ (YFLoginApi *)userFeedbackWithText:(NSString *)text images:(NSArray *)images emial:(NSString *)email {
    
    
    UIDevice *devie = [UIDevice currentDevice];
    NSString *deviceVersion = @"";
    if ([DeviceInfo getLastDevice]) {
        deviceVersion = [DeviceInfo getLastDevice].locationVersion;
    }
    // phoneType 0为ios 1为android
    NSDictionary *dict = @{
                           @"text":[NSString checkIfNullWithString:@"text"],
                           @"screenshots":(images ? images:@[]),
                           @"appVersion":[NSString appCurrentVersion],
                           @"firmwareVersion":deviceVersion,
                           @"phoneVersion":devie.systemVersion,
                           @"phoneType":@"0",
                           @"connect":[NSString checkIfNullWithString:email],
                           };
    
    return [YFLoginApi requestApiWithType:kUserFeedbackApi andParam:dict];
}

/**
 facebook 登录
 */
+ (YFLoginApi *)facebookLoginWithOpenID:(NSString *)openID
                            accessToken:(NSString *)accessToken
                                 gender:(Gender)gender
                              firstName:(NSString *)firstName
                               lastName:(NSString *)lastName
                                    age:(NSString *)age
                                   icon:(NSString *)icon
                               complete:(void(^)(NSError *error, NSString *userID, BOOL isFirstLogin, NSString *token)) complete{
    
    NSDictionary *param = @{@"openId":openID,
                            @"token":accessToken,
                            @"gender":@(gender).stringValue,
                            @"firstName":firstName,
                            @"lastName":lastName,
                            @"age":age?age:@"1990",
                            @"icon":icon
                            };
    YFLoginApi *facebookLoginApi = [YFLoginApi requestApiWithType:kFaceBookLoginApi andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:facebookLoginApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            NSDictionary *errDesc = [result responseInfo];
            NSString *userID = [NSString checkIfNullWithString:errDesc[@"ID"]];
            BOOL isFirst = [[NSString checkIfNullWithString:errDesc[@"IsFirstLogin"]] boolValue];
            NSString *token = [NSString checkIfNullWithString:errDesc[@"Token"]];
            complete(nil, userID, isFirst, token);
        }else if(complete){
            complete(error, nil, NO, nil);
        }
    }];
    return facebookLoginApi;
}
// 找回密码
+ (YFLoginApi *)forgotpasswordWithEmail:(NSString *)email {

    return [YFLoginApi requestApiWithType:kLoginForgotpasswordApi andParam:@{@"email":[NSString checkIfNullWithString:email]}];
}

@end
